<?php
$sanitize_all_escapes = true;
$fake_register_globals = false;
if (!isset($_REQUEST['medico']))
    session_start();

$landingpage = "index.php?site=" . $_SESSION['site_id'];
if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite'])) {
    $pid = $_SESSION['pid'];
} else {
    if (!isset($_REQUEST['medico'])) {
        session_destroy();
        header('Location: ' . $landingpage . '&w');
        exit;
    }
}
$ignoreAuth = true;
require_once('../interface/globals.php');
require_once("$srcdir/patient_provider.inc.php");
require_once("$srcdir/acl.inc");

function is_actual_encounter(){
    if ( $_SESSION['encounter'] > 0 ){
        $query = " SELECT * "
                . " FROM form_encounter "
                . " WHERE encounter = " .$_SESSION['encounter'] . " AND pid = " . $_SESSION['pid']. " ";
        $result = sqlStatement($query);
        if (sqlNumRows($result) > 0 ){
            $row = sqlFetchArray($result);
            if( $row['closed'] == 0 ){
                return true;
            }else{
                return false;
            }
        }else{
            return false;    
        }
    }else{
        return false;
    }
}

if (isset($_REQUEST['medico']))
    //$pid = $_REQUEST['set_pid'];
    $pid = $_SESSION['pid'];
	
$medico = "";
$medicoIni = "";
if (isset($_REQUEST['medico'])) {
    $medico = "&medico=1&set_pid=" . $pid;
    $medicoIni = "?medico=1&set_pid=" . $pid;
}

$sql = "";
//if (!isset($_REQUEST['medico']))
$sql = "SELECT * FROM lists WHERE pid = ? AND type = 'allergy' and source_entry = '0' ORDER BY begdate";
//else
//    $sql = "SELECT * FROM lists WHERE pid = ? AND type = 'allergy' and source_entry = '0' and provider_id = '" . $_SESSION['authUserID'] . "' ORDER BY begdate";
$res = sqlStatement($sql, array($pid));

$i=0;

echo "<h2>Total Alergias";
    if (isset($_REQUEST['medico'])){
        $vall = is_actual_encounter();
        if( $vall ){
            echo "<a class='addIcon css_button css_button_left iframe btnNav fancybox.iframe' href='" . $web_root . "/patients/add_edit_issue.php?issue=0&thistype=allergy" . $medico . "' title='Agregar' id='agregar_alergia'>
            <i class='icon-plus-sign'></i></a>";
        }
    }
        
    echo "<span id='total_alergias' style='float: right; background-color: rgb(228, 228, 228); border-radius: 5px 5px 5px 5px; border-right: 1px solid rgb(204, 204, 204); border-width: 2px 1px 1px; border-style: solid; border-color: -moz-use-text-color rgb(204, 204, 204) rgb(204, 204, 204); -moz-border-top-colors: none; -moz-border-right-colors: none; -moz-border-bottom-colors: none; -moz-border-left-colors: none; border-image: none; padding: 0px 10px;'> " . $i/*sqlNumRows($res)*/ . " </span></h2>";

if (sqlNumRows($res) > 0) {
    ?>
    <ul>
        <?php
        while ($row = sqlFetchArray($res)) {
            $mostrar = true;
            if (isset($_REQUEST['medico']) && $_SESSION['authUserID'] !== $row['provider_id']) {
                $mostrar = existeAutorizacionMedicoInfoPacientes($row['provider_id']);
            }
            if ($mostrar) {
                ?>
                <li>
                    <p>
                        <span class="title" title='<?php echo htmlspecialchars($row['comments'], ENT_NOQUOTES); ?>'><?php echo htmlspecialchars($row['title'], ENT_NOQUOTES); ?></span>
                        <?php if(acl_check('admin','root')){?>
                            <a id="borrar<?php echo $row['id'] ?>" class="enlace_borrar iframe_grafico btnNav fancybox.iframe" title="Borrar" href="<?php echo $web_root; ?>/patients/deleter.php?issue=<?php
                            echo $row['id'];
                            echo $medico
                            ?>;">
                                <i class="icon-remove"></i>
                            </a>
                        <?php } ?>
                        <br/>
                        <span class="desc">
                            <?php
                            if ($row['begdate'] != "") {
                                echo htmlspecialchars(toDateView($row['begdate']), ENT_NOQUOTES);
                                if ($row['enddate'] != "")
                                    echo " - " . htmlspecialchars(toDateView($row['enddate']), ENT_NOQUOTES);
                                echo "</br>";
                            }
                            else
                                echo "Fecha no registrada</br>";
                            ?>
                        </span>
                    </p>
                </li>
                <script type="text/javascript" language="JavaScript">

                    $(document).ready(function() {
            <?php
            if ($row['source_entry'] != '1' && !isset($_REQUEST['medico']))
                echo "$('#borrar" . $row['id'] . "').hide();";
            ?>
                        $('#borrar<?php echo $row['id'] ?>').click(function(e) {
                            e.preventDefault();
                            parent.$.fancybox({
                                href: this.href,
                                autoSize: true,
                                type: 'iframe',
                                helpers: {
                                    overlay: {
                                        opacity: 0.3,
                                        css: {
                                            'background': 'rgba(255, 254, 254,0.85)'
                                        }
                                    } // overlay
                                }, // helpers
                                afterClose: function() {
                                    $("#alergias").load("<?php echo $web_root; ?>/patients/get_allergies.php<?php echo $medicoIni; ?>");
                                    $("#consulta_medica").load("<?php echo $web_root?>/interface/patient_file/encounter/forms.php?set_encounter=<?php echo $_SESSION['encounter'];?>");
                                }//afterClose
                            }); // fancybox
                        }); // click
                    });
                    /********************************************************/
                </script>
                <?php
                $i++;
            }
        }
        echo "</ul>";
    } else {
        echo "<ul><li><p>" . htmlspecialchars(xl("No Results"), ENT_NOQUOTES) .
        "</p></li></ul>";
    }
    ?>

    <script type="text/javascript" language="JavaScript">
        $(document).ready(function() {
            $("#total_alergias").html(<?php echo $i;?>);
            
            $('#agregar_alergia').click(function(e) {
                e.preventDefault();
                parent.$.fancybox({
                    href: this.href,
                    maxWidth: 800,
                    maxHeight: 290,
                    fitToView: false,
                    type: 'iframe',
                    iframe: {
                           preload: false // fixes issue with iframe and IE
                    },
                    'scrolling': 'no',
                    helpers: {
                        overlay: {
                            opacity: 0.3,
                            css: {
                                'background': 'rgba(255, 254, 254,0.85)',
                                'overflow': 'hidden'
                            }
                        } // overlay
                    }, // helpers

                    afterClose: function() {
                        //$("#alergias").load("<?php echo $web_root; ?>/patients/get_allergies.php<?php echo $medicoIni; ?>");
                        //$("#consulta_medica").load("<?php echo $web_root?>/interface/patient_file/encounter/forms.php?set_encounter=<?php echo $_SESSION['encounter'];?>");
                    }//afterClose
                }); // fancybox
            }); // click
        });
        /********************************************************/
    </script>
    <!--[if lt IE 7]>
    <style type='text/css'>
            #simplemodal-container a.modalCloseImg {
                    background:none;
                    right:-14px;
                    width:22px;
                    height:26px;
                    filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(
                            src='img/x.png', sizingMethod='scale'
                    );
            }
    </style>
    <![endif]-->	

    <?php
   /* $existeAutorizacionDetalles = existeAutorizacionSeccionesPaciente($_SESSION['authUserID'], $pid);
    if ($existeAutorizacionDetalles) {
        $sql = "SELECT * FROM lists WHERE pid = ? AND type = 'allergy' and source_entry = '1'  ORDER BY begdate";
        $res = sqlStatement($sql, array($pid));
        if (sqlNumRows($res) > 0) {
            echo "<h2>Registrado por Paciente";
            if (!isset($_REQUEST['medico']))
                echo "<a class='addIcon css_button_left iframe btnNav fancybox.iframe' href='" . $web_root . "/patients/add_edit_issue.php?issue=0&thistype=allergy" . $medico . "' title='Agregar' id='agregar_alergia'>
            <i class='icon-plus-sign'></i></a>";
            echo "<span style='float: right; background-color: rgb(228, 228, 228); border-radius: 5px 5px 5px 5px; border-right: 1px solid rgb(204, 204, 204); border-width: 2px 1px 1px; border-style: solid; border-color: -moz-use-text-color rgb(204, 204, 204) rgb(204, 204, 204); -moz-border-top-colors: none; -moz-border-right-colors: none; -moz-border-bottom-colors: none; -moz-border-left-colors: none; border-image: none; padding: 0px 10px;'> " . sqlNumRows($res) . " </span></h2>";
            ?>
            <ul>
                <?php
                while ($row = sqlFetchArray($res)) {
                    ?>
                    <li>
                        <p>
                            <span class="title" title='<?php echo htmlspecialchars($row['comments'], ENT_NOQUOTES); ?>'><?php echo htmlspecialchars($row['title'], ENT_NOQUOTES); ?></span>
                            <a id="borrar<?php echo $row['id'] ?>" class="enlace_borrar iframe_grafico btnNav fancybox.iframe" title="Borrar" href="<?php echo $web_root; ?>/patients/deleter.php?issue=<?php
                            echo $row['id'];
                            echo $medico
                            ?>;">
                                <i class="icon-remove"></i>
                            </a>
                            <br/>
                            <span class="desc">
                                <?php
                                if ($row['begdate'] != "") {
                                    echo htmlspecialchars(toDateView($row['begdate']), ENT_NOQUOTES);
                                    if ($row['enddate'] != "")
                                        echo " - " . htmlspecialchars(toDateView($row['enddate']), ENT_NOQUOTES);
                                    echo "</br>";
                                }
                                else
                                    echo "Fecha no registrada</br>";
                                ?>
                            </span>
                        </p>
                    </li>
                    <script type="text/javascript" language="JavaScript">

                        $(document).ready(function() {
            <?php
            if ($row['source_entry'] != '1' && !isset($_REQUEST['medico']))
                echo "$('#borrar" . $row['id'] . "').hide();";
            ?>
                            $('#borrar<?php echo $row['id'] ?>').click(function(e) {
                                e.preventDefault();
                                parent.$.fancybox({
                                    href: this.href,
                                    autoSize: true,
                                    type: 'iframe',
                                    helpers: {
                                        overlay: {
                                            opacity: 0.3,
                                            css: {
                                                'background': 'rgba(255, 254, 254,0.85)'
                                            }
                                        } // overlay
                                    }, // helpers
                                    afterClose: function() {
                                        $("#alergias").load("<?php echo $web_root; ?>/patients/get_allergies.php<?php echo $medicoIni; ?>");
                                        $("#consulta_medica").load("<?php echo $web_root?>/interface/patient_file/encounter/forms.php?set_encounter=<?php echo $_SESSION['encounter'];?>");
                                    }//afterClose
                                }); // fancybox
                            }); // click
                        });
                        
                    </script>
                    <?php
                }
                echo "</ul>";
            } else {
                echo "<h2>Registrados por Paciente";
                if (!isset($_REQUEST['medico']))
                    echo "<a class='addIcon css_button_left iframe btnNav fancybox.iframe' href='" . $web_root . "/patients/add_edit_issue.php?issue=0&thistype=allergy" . $medico . "' title='Agregar' id='agregar_alergia'>
                       <i class='icon-plus-sign'></i></a>";
                echo "<span style='float: right; background-color: rgb(228, 228, 228); border-radius: 5px 5px 5px 5px; border-right: 1px solid rgb(204, 204, 204); border-width: 2px 1px 1px; border-style: solid; border-color: -moz-use-text-color rgb(204, 204, 204) rgb(204, 204, 204); -moz-border-top-colors: none; -moz-border-right-colors: none; -moz-border-bottom-colors: none; -moz-border-left-colors: none; border-image: none; padding: 0px 10px;'> " . sqlNumRows($res) . " </span></h2>";

                echo "<ul><li><p>" . htmlspecialchars(xl("No Results"), ENT_NOQUOTES) .
                "</p></li></ul>";
            }
        }*/
        ?>
